import tkinter as tk
import webbrowser
import register
class DynamicEntryForm(tk.Tk):
    def __init__(self,name):
        super().__init__()
        self.title("寄存器信息录入助手")
        self.name = name
        # 创建一个Frame用于放置按钮
        button_frame = tk.Frame(self)
        button_frame.pack(side=tk.TOP, fill=tk.X)

        # 创建一个Frame用于放置输入框和标签
        self.entry_frame = tk.Frame(self)
        self.entry_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.create_widgets()
        # 创建一个列表用于存储输入框
        self.entries = []

        # 添加第一组输入框，并带有标签
        self.add_first_entry()
        
    def create_widgets(self):
        # 创建一个框架来容纳按钮
        button_frame = tk.Frame(self)
        button_frame.pack(fill=tk.X)

        # 创建一个按钮，点击时会添加新的输入框组
        add_button = tk.Button(button_frame, text="添加新组(d)", command=self.add_entry)
        add_button.pack(side=tk.LEFT, padx=(10, 10))

        # 创建一个按钮，点击后会打开一个网站
        website_button = tk.Button(button_frame, text="开源地址(u)", command=self.open_website)
        website_button.pack(side=tk.RIGHT, padx=(0, 10))  # 添加到右侧，右边距10

        # 创建一个按钮，点击后会打印所有非空输入框的内容
        finish_button = tk.Button(button_frame, text="完成(s)", command=self.create_register)
        finish_button.pack(side=tk.RIGHT, padx=(10, 20))  # 添加到右侧，左边距10，右边距20

        # 绑定快捷键
        self.bind('<Control-d>', lambda event: self.add_entry())
        self.bind('<Control-u>', lambda event: self.open_website())
        self.bind('<Control-s>', lambda event: self.create_register())
    def open_website(self):
        # 在这里设置您想要打开的网站URL
        url = "https://gitee.com/waterguy/register-ui-friend-by-c"
        webbrowser.open_new(url)  # 打开新的浏览器窗口
    def add_first_entry(self):
        # 创建一个子Frame用于放置一组输入框和标签
        frame = tk.Frame(self.entry_frame)
        frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)

        # 创建标签框架并添加标签
        labels_frame = tk.Frame(frame)
        labels_frame.pack(side=tk.TOP, fill=tk.X)
        tk.Label(labels_frame, text="Name:", width=15).pack(side=tk.LEFT, padx=(0, 5))
        tk.Label(labels_frame, text="Address:", width=15).pack(side=tk.LEFT)

        # 创建输入框并添加到子Frame中
        name_entry = tk.Entry(frame, width=15)
        name_entry.pack(side=tk.LEFT, padx=(0, 5))

        address_entry = tk.Entry(frame, width=15)
        address_entry.pack(side=tk.LEFT)

        # 将输入框添加到列表中，以便于管理
        self.entries.append((name_entry, address_entry))

    def add_entry(self):
        # 创建一个子Frame用于放置一组输入框
        frame = tk.Frame(self.entry_frame)
        frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)

        # 创建输入框并添加到子Frame中
        name_entry = tk.Entry(frame, width=15)
        name_entry.pack(side=tk.LEFT, padx=(0, 5))

        address_entry = tk.Entry(frame, width=15)
        address_entry.pack(side=tk.LEFT)

        # 将输入框添加到列表中，以便于管理
        self.entries.append((name_entry, address_entry))

    def print_entries(self):
        for name_entry, address_entry in self.entries:
            name = name_entry.get().strip()
            address = address_entry.get().strip()
            # 如果输入框为空，则不打印该组数据
            if name or address:
                print(f"Name: {name}, Address: {address}")
    def create_register(self):
    # 打印文本框中的内容
        creator = register.RegisterFileCreator(self.name)
        for name_entry, address_entry in self.entries:
            name = name_entry.get().strip()
            address_str = address_entry.get().strip()
            # 如果输入框为空，则不打印该组数据
            if name and address_str:
                try:
                    # 去除可能的 "0x" 前缀
                    if address_str.startswith("0x"):
                        address_str = address_str[2:]
                    # 尝试将地址转换为整数
                    address = int(address_str, 16)  # 假设地址是以十六进制提供的
                    print(f"Name: {name}, Address: {address:X}")  # 打印十六进制地址
                    creator.create_register_files(f"{name}", address)
                except ValueError:
                    # 如果转换失败，打印错误消息
                    print(f"无法将地址 '{address_str}' 转换为整数。请输入有效的十六进制地址。")


if __name__ == "__main__":
    app = DynamicEntryForm("example")
    app.mainloop()
